package org.igeek.atomqq.task;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import org.apache.http.util.ByteArrayBuffer;
import org.igeek.atomqq.activity.BaseActivity;
import org.igeek.atomqq.net.HttpConnection;
import org.igeek.atomqq.net.HttpConnection.Request_TYPE;
import org.igeek.atomqq.net.Response;
import org.igeek.atomqq.util.URLManager;
import org.json.JSONException;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;


/**
 * 获取验证码图片
 * 正常返回类型: image/jpeg
 * @author hangxin1940@gmail.com
 * @version 创建时间：2012-1-29 下午1:32:26
 * 
 */
public class CheckcodeImageTask extends SingleAsyncTask<Void,Void,Void>{
	private TaskResultListener listener;
	private CheckCodeResponse response;
	private String uin;
	private String code;
	private int flag;
	
	private Handler handler=new Handler(){
		
		
		@Override
		public void handleMessage(Message msg) {
			
			activity.showDialog(BaseActivity.DIALOG_ERROR, msg.getData());
		}
	};
	
	private BaseActivity activity;
	
	
	public CheckcodeImageTask(BaseActivity activity) {
		this.activity=activity;
	}
	
	public void setOnTaskResult(TaskResultListener listener,int flag){
		this.listener=listener;
		this.flag=flag;
	}
	
	public void setParams(String uin,String code){
		this.uin=uin;
		this.code=code;
	}

	@Override
	protected Void doInBackground(Void... params) {
		try {
			String surl=URLManager.CHECK_CODE_P+"?"+"aid=1003903&uin="+uin+"&vc_type="+code;
			URL url = new URL(surl);
			
			HttpConnection conn=new HttpConnection(url, Request_TYPE.GET);
			conn.setReferer(URLManager.REFER_ui_ptlogin2_qq_com_login);
			conn.connect();
			
			
			Response res=conn.getResponse();
			InputStream ins=res.getStream();
			
			
			BufferedInputStream instream=new BufferedInputStream(ins);
			ByteArrayBuffer baf=new ByteArrayBuffer(50);
	        int current=0;
			while((current=instream.read())!=-1){
				
				baf.append((byte)current);
				
				
			}
			byte[] bytes=baf.toByteArray();
			Bitmap image=BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
			
			response=new CheckCodeResponse();
			response.setBitmap(image);
			conn.disconnect();
			
			
		}catch (UnknownHostException e) {
			e.printStackTrace();
			showErrorDialog("网络异常");
		}catch (MalformedURLException e) {
			e.printStackTrace();
			showErrorDialog("连接出错");
		} catch (IOException e) {
			e.printStackTrace();
			showErrorDialog("数据读取异常");
		} catch (JSONException e) {
			e.printStackTrace();
			showErrorDialog("服务器返回出错");
		}
		
		
		return null;
	}
	
	private void showErrorDialog(String message){
		Bundle bundle=new Bundle();
		bundle.putString(BaseActivity.FLAG_ERROR, message);
		
		Message msg=new Message();
		msg.setData(bundle);
		handler.sendMessage(msg );
	}
	
	@Override
	protected void onPostExecute(Void result) {
		listener.onTaskResult(response,flag);
	}

	public class CheckCodeResponse extends Response{
		private Bitmap bitmap;

		public Bitmap getBitmap() {
			return bitmap;
		}

		public void setBitmap(Bitmap bitmap) {
			this.bitmap = bitmap;
		}
		
	}


}
